package com.synv.flashcard;

import com.synv.entity.FlashCard;
import com.synv.flashcard.R.color;
import com.synv.util.DataBaseHelper;

import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.ColorDrawable;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

public class WordDetailActivity extends Activity {

	private FlashCard flashcard;
	private TextView txtEditText;
	private EditText txtMeaning, txtNotes;
	private CheckBox noun, verb, adj, adv, prep, other;
	private Button btnSaveWord;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_word_detail);
		
		ActionBar actionBar = getActionBar();
		actionBar.setBackgroundDrawable(new ColorDrawable(color.header));
		actionBar.setTitle("Edit Word");
		
		Intent intent = getIntent();
		String word = intent.getStringExtra("word");
		DataBaseHelper myDbHelper = new DataBaseHelper(this);
		Cursor c = myDbHelper.getReadableDatabase().query("flashcard", null, "word='" + word + "'", null, null, null, null);
 		c.moveToFirst();
 		flashcard = new FlashCard(c.getInt(0), c.getString(1), 
					c.getInt(2), c.getString(3), c.getString(4), 
					c.getString(5), c.getString(6), c.getString(7), 
					c.getInt(8), c.getInt(9), c.getInt(10), c.getString(11));
 		
 		txtEditText = (TextView) findViewById(R.id.txtEditText);
 		txtMeaning = (EditText) findViewById(R.id.txtMeaning);
 		txtNotes = (EditText) findViewById(R.id.txtNotes);
 		noun = (CheckBox) findViewById(R.id.noun);
 		verb = (CheckBox) findViewById(R.id.verb);
 		adj = (CheckBox) findViewById(R.id.adj);
 		adv = (CheckBox) findViewById(R.id.adv);
 		prep = (CheckBox) findViewById(R.id.prep);
 		other = (CheckBox) findViewById(R.id.other);
 		btnSaveWord = (Button) findViewById(R.id.btnSaveWord);
 		
 		txtEditText.setText(flashcard.getWord());
 		txtMeaning.setText(flashcard.getMeaning());
 		txtNotes.setText(flashcard.getExamples());
 		
 		setWordType(flashcard.getWordType());
 		
 		btnSaveWord.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				saveWord();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.word_detail, menu);
		return true;
	}
	
	
	
	private void setWordType(Integer wordType) {
		if(wordType >= FlashCard.PREP) {
			prep.setChecked(true);
			wordType = wordType - FlashCard.PREP;
		}
		if(wordType >= FlashCard.ADV) {
			adv.setChecked(true);
			wordType = wordType - FlashCard.ADV;
		}
		if(wordType >= FlashCard.ADJ) {
			adj.setChecked(true);
			wordType = wordType - FlashCard.ADJ;
		}
		if(wordType >= FlashCard.VERB) {
			verb.setChecked(true);
			wordType = wordType - FlashCard.VERB;
		}
		if(wordType >= FlashCard.NOUN) {
			noun.setChecked(true);
			wordType = wordType - FlashCard.NOUN;
		}
	}

	private Integer getWordType() {
		Integer type = 0;
		if(noun.isChecked()) type += FlashCard.NOUN;
		if(verb.isChecked()) type += FlashCard.VERB;
		if(adj.isChecked()) type += FlashCard.ADJ;
		if(adv.isChecked()) type += FlashCard.ADV;
		if(prep.isChecked()) type += FlashCard.PREP;
		return type;
	}
	
	private void saveWord() {
		DataBaseHelper myDbHelper = new DataBaseHelper(getApplicationContext());
		myDbHelper.getWritableDatabase().execSQL("UPDATE flashcard SET WordType=" + getWordType() + ", Meaning='" + txtMeaning.getText().toString().replaceAll("'", "''") + "',Examples = '" + txtNotes.getText().toString().replaceAll("'", "''") + "' WHERE word='" + flashcard.getWord() + "'");
		myDbHelper.close();
		finish();
	}
}
